package com.crane.common.framework.utils;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日期工具类
 *
 * @author zhenxing.pi
 */
public class DateUtil {

	public static final String FULL_TIMESTAMP = "yyyyMMddHHmmssSSS";

	public static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static final String TIME_PATTERN_NO_SPLIT = "yyyyMMddHHmmss";

	public static final String TIME_PATTERN1 = "yyyyMMdd HH:mm:ss";

	public static final String TIME_PATTERN_CH = "yyyy年MM月dd日 HH时mm分ss秒";

	public static final String DATE_PATTERN = "yyyy-MM-dd";

	public static final String DATE_PATTERN_SHORT = "yyMMdd";

	public static final String DATE_PATTERN_1 = "yyyyMMdd";

	public static final String DATE_PATTERN_2 = "yyyy/MM/dd";

	public static final String DATE_PATTERN_3 = "yyyyMM";

	public static final String DATE_PATTERN_CH = "yyyy年MM月dd日";

	/**
	 * 当前日期(yyyy-MM-dd)
	 */
	public static int CUR_DATE = 0;
	/**
	 * 往后一天(yyyy-MM-dd)
	 */
	public static int CUR_BEFORE = 1;
	/**
	 * 往前一天(yyyy-MM-dd)
	 */
	public static int CUR_AFTER = -1;

	private static final int[] TIME_FIELD_LEVELS = { Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR_OF_DAY,
			Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND };

	private static String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

	private static final Map<String, Integer> PERIOD_UNITS;

	private static final Pattern TERM_PATTERN = Pattern.compile("^[ ]*([0-9]+)[ ]*([Y|M|W|D|h|m|s]).*$");

	static {
		PERIOD_UNITS = new HashMap<String, Integer>();
		PERIOD_UNITS.put("Y", new Integer(Calendar.YEAR));
		PERIOD_UNITS.put("M", new Integer(Calendar.MONTH));
		PERIOD_UNITS.put("W", new Integer(Calendar.WEEK_OF_MONTH));
		PERIOD_UNITS.put("D", new Integer(Calendar.DATE));
		PERIOD_UNITS.put("h", new Integer(Calendar.HOUR_OF_DAY));
		PERIOD_UNITS.put("m", new Integer(Calendar.MINUTE));
		PERIOD_UNITS.put("s", new Integer(Calendar.SECOND));
	}

	/**
	 * 得到指定时间的00：00：00
	 *
	 * @param date
	 * @return
	 */
	public static String getStartDateStr(java.util.Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(11, 0);
		c.set(12, 0);
		c.set(13, 0);
		c.set(14, 0);
		return format(TIME_PATTERN, c.getTime());
	}

	/**
	 * 得到指定时间的23：59：59
	 *
	 * @param date
	 * @return
	 */
	public static String getEndDateStr(java.util.Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(11, 23);
		c.set(12, 59);
		c.set(13, 59);
		c.set(14, 999);
		return format(TIME_PATTERN, c.getTime());
	}

	/**
	 * String(yyyyMMdd) -> Date
	 *
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDbDate(String strDate) {
		if (strDate == null) {
			return null;
		}
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(DATE_PATTERN_1);
		try {
			return FORMAT_DATE_DB.parse(strDate);
		} catch (ParseException e) {
			throw new RuntimeException("将字符串" + strDate + "解析为" + FORMAT_DATE_DB.toPattern() + "格式的日期时发生异常:", e);
		}
	}

	/**
	 * String(yyyyMMddHHmmss) -> Date
	 *
	 * @param dateTime 时间字符串(yyyyMMddHHmmss)
	 * @return
	 */
	public static Date parseBackendDateTime(String dateTime) {
		if (dateTime == null) {
			return null;
		}
		SimpleDateFormat FORMAT_DATETIME_BACKEND = new SimpleDateFormat(TIME_PATTERN_NO_SPLIT);
		try {
			return FORMAT_DATETIME_BACKEND.parse(dateTime);
		} catch (ParseException e) {
			throw new RuntimeException("将字符串" + dateTime + "解析为" + FORMAT_DATETIME_BACKEND.toPattern() + "格式的日期时发生异常:",
					e);
		}
	}

	/**
	 * String(yyyy-MM-dd HH:mm:ss) -> Date
	 *
	 * @param dateTime 时间字符串(yyyy-MM-dd HH:mm:ss)
	 * @return
	 */
	public static Date parseFullDateTime(String dateTime) {
		if (dateTime == null) {
			return null;
		}
		SimpleDateFormat FORMAT_FULL_DATETIME = new SimpleDateFormat(TIME_PATTERN);
		try {
			return FORMAT_FULL_DATETIME.parse(dateTime);
		} catch (ParseException e) {
			throw new RuntimeException("将字符串" + dateTime + "解析为" + FORMAT_FULL_DATETIME.toPattern() + "格式的日期时发生异常:", e);
		}
	}

	/**
	 * Date -> String(YYYYMMDD)
	 *
	 * @param date
	 * @return
	 */
	public static String formatPageDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		return FORMAT_DATE_PAGE.format(date);
	}

	/**
	 * Date -> String(yyyy-MM-dd HH:mm:ss)
	 *
	 * @param date
	 * @return
	 */
	public static String formatFullDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_FULL_DATETIME = new SimpleDateFormat(TIME_PATTERN);
		return FORMAT_FULL_DATETIME.format(date);
	}

	/**
	 * Date -> String(yyyyMMddHHmmss)
	 *
	 * @param date
	 * @return
	 */
	public static String formatFull2Date(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_DATETIME_BACKEND = new SimpleDateFormat(TIME_PATTERN_NO_SPLIT);
		return FORMAT_DATETIME_BACKEND.format(date);
	}

	/**
	 * String(YYYY-MM-DD)-> String(YYYYMMDD)
	 *
	 * @param pageDate
	 * @return
	 */
	public static String convertDate4Page2DB(String pageDate) {
		if (pageDate == null) {
			return "";
		}
		if (pageDate.length() != 10) {
			return pageDate;
		}
		return pageDate.replaceAll("-", "");
	}

	/**
	 * String(YYYYMMDD)->String(YYYY-MM-DD)
	 *
	 * @param dbDate
	 * @return
	 */
	public static String convertDate4DB2Page(String dbDate) {
		if (dbDate == null) {
			return "";
		}
		if (dbDate.length() != 8) {
			return dbDate;
		} else {
			return dbDate.substring(0, 4) + "-" + dbDate.substring(4, 6) + "-" + dbDate.substring(6, 8);
		}
	}

	/**
	 * String(YYYYMM)->String(YYYY-MM)
	 *
	 * @param dbDate
	 * @return
	 */
	public static String convertDate2DB2Page(String dbDate) {
		if (dbDate == null) {
			return "";
		}
		if (dbDate.length() != 6) {
			return dbDate;
		} else {
			return dbDate.substring(0, 4) + "-" + dbDate.substring(4, 6);
		}
	}

	/**
	 * String(HHMMSS) -> String(HH-MM-SS)
	 *
	 * @param dbDate
	 * @return
	 */
	public static String convertTime4DB2Page(String dbDate) {
		if (dbDate == null) {
			return "";
		}
		if (dbDate.length() != 6) {
			return dbDate;
		} else {
			return dbDate.substring(0, 2) + ":" + dbDate.substring(2, 4) + ":" + dbDate.substring(4, 6);
		}
	}

	/**
	 * String(HH-MM-SS) -> String(HHMMSS)
	 *
	 * @param pageTime
	 * @return
	 */
	public static String convertTime4Page2DB(String pageTime) {
		if (pageTime == null) {
			return "";
		}
		if (pageTime.length() != 8) {
			return pageTime;
		}
		return pageTime.replaceAll(":", "");
	}

	/**
	 * String(HHmmss) -> String(HH:mm:ss)
	 *
	 * @param dbTime
	 * @return
	 */
	public static String dbTimeToPageTime(String dbTime) throws ParseException {
		if (dbTime == null) {
			return "";
		}
		SimpleDateFormat FORMAT_TIME_PAGE = new SimpleDateFormat("HH:mm:ss");
		SimpleDateFormat FORMAT_TIME = new SimpleDateFormat("HHmmss");
		return FORMAT_TIME_PAGE.format(FORMAT_TIME.parse(dbTime));
	}

	/**
	 * String(YYYYMMddHHmmss) -> String(YYYY-MM-DD HH:mm:ss)
	 *
	 * @param dbTime
	 * @return
	 * @throws ParseException
	 */
	public static String dbTimeToPageTimeFull(String dbTime) throws ParseException {
		if (dbTime == null) {
			return "";
		}
		SimpleDateFormat FORMAT_TIME_PAGE = new SimpleDateFormat(TIME_PATTERN);
		SimpleDateFormat FORMAT_TIME = new SimpleDateFormat(TIME_PATTERN_NO_SPLIT);
		return FORMAT_TIME_PAGE.format(FORMAT_TIME.parse(dbTime));
	}

	/**
	 * String(YYYY-MM-DD HH:mm:ss) -> String(YYYYMMddHHmmss)
	 *
	 * @param pageDate
	 * @return
	 */
	public static String convertDateFullPage2DB(String pageDate) {
		if (pageDate == null) {
			return "";
		}
		if (pageDate.length() != 19) {
			return pageDate;
		}
		pageDate = pageDate.replaceAll(":", "");
		pageDate = pageDate.replaceAll("-", "");
		pageDate = pageDate.replaceAll(" ", "");
		return pageDate;
	}

	/**
	 * 把日期，时间转化为格式：yyyy-MM-dd HH:mm:ss
	 *
	 * @param date 日期，格式：yyyyMMdd
	 * @param time 时间，格式：HHmmss
	 * @return
	 */
	public static String getDateTime(String date, String time) {
		StringBuffer sb = new StringBuffer();
		sb.append(convertDate4DB2Page(date));
		sb.append(" ");

		try {
			sb.append(dbTimeToPageTime(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sb.toString();
	}

	/**
	 * 把日期，时间转化为格式：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getDateTime() {
		SimpleDateFormat FORMAT_FULL_DATETIME = new SimpleDateFormat(TIME_PATTERN);
		return FORMAT_FULL_DATETIME.format(new Date());
	}

	/**
	 * 取得当前日期字符串; 日期格式:yyyyMMdd
	 *
	 * @return
	 */
	public static String getCurrentDate() {
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(DATE_PATTERN_1);
		return FORMAT_DATE_DB.format(new Date());
	}

	/**
	 * 取得当前日期字符串; 日期格式:yyyy-MM-dd
	 *
	 * @return
	 */
	public static String getCurrentPageDate() {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		return FORMAT_DATE_PAGE.format(new Date());
	}

	/**
	 * 取得当前时间字符串; 时间格式:hhmmss
	 *
	 * @return
	 */
	public static String getCurrentTime() {
		SimpleDateFormat FORMAT_TIME = new SimpleDateFormat("HHmmss");
		return FORMAT_TIME.format(new Date());
	}

	/**
	 * 取得当前时间字符串; 时间格式:hhmmss
	 *
	 * @param date
	 * @return
	 */
	public static String getCurrentTime(Date date) {
		SimpleDateFormat FORMAT_TIME = new SimpleDateFormat("HHmmss");
		return FORMAT_TIME.format(date);
	}

	/**
	 * 取得当前时间字符串; 时间格式:HHmmssSSS
	 *
	 * @return
	 */
	public static String getCurrentTimeMilliSecond() {
		SimpleDateFormat FORMAT_TIME_MILLISECOND = new SimpleDateFormat("HHmmssSSS");
		return FORMAT_TIME_MILLISECOND.format(new Date());
	}

	/**
	 * 取得当前时间字符串; 时间格式:HHmmssSSS
	 *
	 * @param date
	 * @return
	 */
	public static String getCurrentTimeMilliSecond(Date date) {
		SimpleDateFormat FORMAT_TIME_MILLISECOND = new SimpleDateFormat("HHmmssSSS");
		return FORMAT_TIME_MILLISECOND.format(date);
	}

	/**
	 * 解析时间字符串;
	 *
	 * @param time
	 * @return
	 */
	public static Date parseTime(String time) {
		SimpleDateFormat FORMAT_TIME = new SimpleDateFormat("HHmmss");
		try {
			return FORMAT_TIME.parse(time);
		} catch (ParseException e) {
			throw new RuntimeException("将字符串" + time + "按照" + FORMAT_TIME.toPattern() + "格式进行解析时发生异常:", e);
		}
	}

	/**
	 * Date -->> yyyy年MM月dd HH:mm:ss
	 *
	 * @param date
	 */
	public static String formatLocalDate(Date date) {
		SimpleDateFormat FORMAT_LOCAL = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
		return FORMAT_LOCAL.format(date);
	}

	/**
	 * HH:mm:ss ->> HHmmss
	 *
	 * @param pageTime
	 * @return
	 */
	public static String pageTimeToDbTime(String pageTime) {
		return pageTime.replaceAll(":", "");
	}

	/**
	 * 将日期转换为指定格式
	 *
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formateDate2Str(Date date, String pattern) {
		SimpleDateFormat s = new SimpleDateFormat(pattern);
		return s.format(date);
	}

	/**
	 * 将日期中的2007-1-1转化为20070101格式
	 *
	 * @param datestr
	 * @return
	 */
	public static String dateStringFormat(String datestr) {
		if (datestr == null || datestr.equals(""))
			return null;
		String[] str1 = datestr.split("-");
		if (str1.length == 3) {
			if (str1[1].length() == 1) {
				str1[1] = "0" + str1[1];
			}
			if (str1[2].length() == 1) {
				str1[2] = "0" + str1[2];
			}
		} else {
			return datestr;
		}
		datestr = str1[0] + str1[1] + str1[2];
		return datestr;
	}

	public static Date addDay(Date oriDate, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(oriDate);
		cal.add(Calendar.DAY_OF_MONTH, amount);
		return cal.getTime();
	}

	/**
	 * 月份往前或者往后
	 */
	public static Date addMonth(Date oriDate, int amount) {
		Calendar c = Calendar.getInstance();
		c.setTime(oriDate);
		c.add(Calendar.MONTH, amount);
		return c.getTime();
	}

	/**
	 * 把日期加上小时得出最后的时间，时间转化为格式：yyyy-MM-dd HH:mm:ss
	 *
	 * @param dateTime 日期，格式：yyyy-MM-dd HH24:MI:SS
	 * @return
	 */
	public static Date getAddDateTime(Date dateTime, int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);
		calendar.add(Calendar.HOUR, hours);
		return calendar.getTime();
	}

	/**
	 * 把日期加上小时得出最后的时间，时间转化为格式：yyyy-MM-dd HH:mm:ss
	 *
	 * @param dateTime 日期，格式：yyyyMMdd
	 * @param hours
	 * @return
	 */
	public static String getAddDateTime(String dateTime, int hours) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat FORMAT_FULL_DATETIME = new SimpleDateFormat(TIME_PATTERN);
		try {
			calendar.setTime(new SimpleDateFormat(DATE_PATTERN_1).parse(dateTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.HOUR, hours);
		return FORMAT_FULL_DATETIME.format(calendar.getTime());
	}

	/**
	 * 将日期中的20070101转化为2007-01-01 00:00:00格式
	 *
	 * @param dateValue
	 * @return
	 */
	public static Date parseFullDate(String dateValue) {
		Calendar ca = Calendar.getInstance();
		int year = Integer.parseInt(dateValue.substring(0, 4));// 年
		ca.set(Calendar.YEAR, year);
		int month = Integer.parseInt(dateValue.substring(4, 6));// 月
		ca.set(Calendar.MONTH, month - 1);
		int day = Integer.parseInt(dateValue.substring(6, 8));// 日
		ca.set(Calendar.DAY_OF_MONTH, day);
		Date date = ca.getTime();
		return date;
	}

	/**
	 * 取得指定格式的当前时间
	 *
	 * @param pattern
	 * @return
	 */
	public static String getTime(String pattern) {
		return new SimpleDateFormat(pattern).format(new Date());
	}

	/**
	 * 取得指定时间的偏移时间
	 *
	 * @param transferTime 原始时间（yyyy-MM-dd HH:ss:mm）
	 * @param calendarType 偏移单位（Calendar的常量）
	 * @param i            偏移量
	 * @return
	 */
	public static String getExcursionTime(String transferTime, int calendarType, int i) {
		SimpleDateFormat FORMAT_FULL_DATETIME = new SimpleDateFormat(TIME_PATTERN);
		Date parseFullDateTime = parseFullDateTime(transferTime);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parseFullDateTime);
		calendar.add(calendarType, i);
		return FORMAT_FULL_DATETIME.format(calendar.getTime());
	}


	public static String getExcursionTime(int calendarType, int i) {
		SimpleDateFormat FORMAT_FULL_DATETIME = new SimpleDateFormat(TIME_PATTERN);
		Calendar calendar = Calendar.getInstance();
		calendar.add(calendarType, i);
		return FORMAT_FULL_DATETIME.format(calendar.getTime());
	}

	/**
	 * 取得指定时间的偏移时间
	 *
	 * @param calendarType 偏移单位（Calendar的常量）
	 * @param i            偏移量
	 * @return
	 */
	public static String getExcursionTime(int calendarType, int i, String pattern) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(calendarType, i);
		return new SimpleDateFormat(pattern).format(calendar.getTime());
	}

	/**
	 * 取得当前小时和分钟构成的长整型数组,例如(12:30 = 1230)
	 *
	 * @return
	 */
	public static Long getCurrentHourMinute() {
		SimpleDateFormat FORMAT_HOUR_MINUTE = new SimpleDateFormat("HHmm");
		return Long.parseLong(FORMAT_HOUR_MINUTE.format(new Date()));
	}

	/**
	 * 取得当前时间的偏移时间 格式为（yyyyMMdd）+1为往后一天，-1为向前一天，0为当天
	 */
	public static String getAboutDate(int i) {
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(DATE_PATTERN_1);
		Calendar rightNow = Calendar.getInstance();
		rightNow.add(Calendar.DAY_OF_MONTH, i);
		String mDateTime = FORMAT_DATE_DB.format(rightNow.getTime());
		return mDateTime;
	}

	/**
	 * 取得当前时间的偏移时间 格式为（yyyy-MM-dd HH:mm:ss）+1为往后一天，-1为向前一天，0为当天
	 */
	public static String getAboutDateFull(int i) {
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(TIME_PATTERN);
		Calendar rightNow = Calendar.getInstance();
		rightNow.add(Calendar.DAY_OF_MONTH, i);
		String mDateTime = FORMAT_DATE_DB.format(rightNow.getTime());
		return mDateTime;
	}

	/**
	 * 时间是否在指定分钟数内
	 *
	 * @param beginTimeStr 指定的时间
	 * @param minutes      指定的分钟
	 * @return
	 */
	public static boolean isWithinMintueTime(String beginTimeStr, int minutes) {
		Date beginTimeObj = parseFullDateTime(beginTimeStr);
		Calendar cal = Calendar.getInstance();
		cal.setTime(beginTimeObj);
		cal.add(Calendar.MINUTE, minutes);
		Date beginTimeAdd5 = cal.getTime();
		if (beginTimeAdd5.after(new Date())) {
			return true;
		}
		return false;
	}

	public static boolean isWithinMintueTime(Date beginTime, int minutes) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(beginTime);
		cal.add(Calendar.MINUTE, minutes);
		Date beginTimeAdd5 = cal.getTime();
		if (beginTimeAdd5.after(new Date())) {
			return true;
		}
		return false;
	}

	/**
	 * Date -> String(yyMMdd)
	 *
	 * @param date
	 * @return
	 */
	public static String formatyMdDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_DATE_YMD = new SimpleDateFormat(DATE_PATTERN_SHORT);
		return FORMAT_DATE_YMD.format(date);
	}

	/**
	 * Date -> String(MMdd)
	 *
	 * @param date
	 * @return
	 */
	public static String formatMdDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_DATE_MD = new SimpleDateFormat("MMdd");
		return FORMAT_DATE_MD.format(date);
	}

	/**
	 * yyyy-MM-dd HH:mm:ss转换成毫秒数
	 *
	 * @param new_date_f
	 * @return
	 */
	public static long conversion(String new_date_f) {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		SimpleDateFormat FORMAT_FULL_DATETIME = new SimpleDateFormat(TIME_PATTERN);
		try {
			long millisecond = FORMAT_FULL_DATETIME.parse(new_date_f).getTime();
			return millisecond;
		} catch (ParseException e) {
			throw new RuntimeException("将字符串" + new_date_f + "按照" + FORMAT_DATE_PAGE + "格式进行转换毫秒时发生异常:", e);
		}
	}

	/**
	 * 当月第一天
	 *
	 * @return
	 */
	public static String monthStart() {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		Calendar curCal = Calendar.getInstance();
		curCal.set(Calendar.DAY_OF_MONTH, 1);
		Date beginTime = curCal.getTime();
		String sTime = FORMAT_DATE_PAGE.format(beginTime) + " 00:00:00";
		return sTime;
	}

	/**
	 * 当月最后一天
	 *
	 * @return
	 */
	public static String monthEnd() {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		Calendar curCal = Calendar.getInstance();
		curCal.set(Calendar.DATE, 1);
		curCal.roll(Calendar.DATE, -1);
		Date endTime = curCal.getTime();
		String eTime = FORMAT_DATE_PAGE.format(endTime) + " 23:59:59";
		return eTime;
	}

	/**
	 * 判断当时系统时间是否在23:30:00~00:00:00 || 00:00:00~00:30:00
	 *
	 * @param sysTime
	 * @return
	 */
	public static Integer chooseDate(Long sysTime) {
		String beginDateStr = formatPageDate(new Date());
		Date beginDate = parseFullDateTime(beginDateStr + " 23:30:00");
		Long beginTime = beginDate.getTime();
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, 1);
		Date date = c.getTime();
		String endDateStr = formatPageDate(date);
		Date endDate = parseFullDateTime(endDateStr + " 00:00:00");
		Long endTime = endDate.getTime();
		Date lastDate = parseFullDateTime(endDateStr + " 00:30:00");
		Long lastTime = lastDate.getTime();
		if (sysTime >= beginTime && sysTime <= endTime) {
			return CUR_AFTER;
		} else if (sysTime >= endTime && sysTime <= lastTime) {
			return CUR_BEFORE;
		} else {
			return CUR_DATE;
		}
	}

	/**
	 * 将日期转移成yyyy-MM-dd HH:mm格式
	 *
	 * @param date
	 * @return
	 */
	public static String convertDateByLessSec(String date) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat FORMAT_LESS_SEC_DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			c.setTime(FORMAT_LESS_SEC_DATETIME.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return FORMAT_LESS_SEC_DATETIME.format(c.getTime());
	}

	public static String formatDateTime(String pattern) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
		String now = sdf.format(new java.util.Date());
		return now;
	}

	public static String formatDateTime(String pattern, java.util.Date date) {
		String strDate = null;
		String strFormat = pattern;
		SimpleDateFormat dateFormat = null;

		if (date == null)
			return "";

		dateFormat = new SimpleDateFormat(strFormat);
		strDate = dateFormat.format(date);

		return strDate;
	}

	public static String formatDateTime(String pattern, java.util.Date date, Locale locale) {
		String strDate = null;
		String strFormat = pattern;
		SimpleDateFormat dateFormat = null;

		if (date == null)
			return "";

		dateFormat = new SimpleDateFormat(strFormat, locale);
		strDate = dateFormat.format(date);

		return strDate;
	}

	public static Date composeDate(Date date, Date time) {
		if (date == null || time == null)
			return null;
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(time);
		Calendar c3 = Calendar.getInstance();
		c3.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DATE), c2.get(Calendar.HOUR_OF_DAY),
				c2.get(Calendar.MINUTE), c2.get(Calendar.SECOND));
		return c3.getTime();
	}

	public static Date getTheDate(Date date) {
		if (date == null)
			return null;
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		Calendar c2 = Calendar.getInstance();
		c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DATE), 0, 0, 0);
		long millis = c2.getTimeInMillis();
		millis = millis - millis % 1000;
		c2.setTimeInMillis(millis);
		return c2.getTime();
	}

	public static Date skipDateTime(Date d, int skipDay) {
		if (d == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		calendar.add(Calendar.DATE, skipDay);
		return calendar.getTime();
	}

	public static boolean dateCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat(DATE_PATTERN);
		String isDate = sdf_d.format(new java.util.Date());
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_d.parse(str);
			date0 = sdf_d.parse(isDate);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			bea = false;
		}
		return bea;
	}

	public static boolean monthCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_m = new SimpleDateFormat("yyyy-MM");
		String isMonth = sdf_m.format(new java.util.Date());
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_m.parse(str);
			date0 = sdf_m.parse(isMonth);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			bea = false;
		}
		return bea;
	}

	public static boolean secondCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat(TIME_PATTERN);
		String isDate = sdf_d.format(new java.util.Date());
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_d.parse(str);
			date0 = sdf_d.parse(isDate);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			bea = false;
		}
		return bea;
	}

	public static boolean secondCompare(String str1, String str2) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat(TIME_PATTERN);
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_d.parse(str1);
			date0 = sdf_d.parse(str2);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			bea = false;
		}
		return bea;
	}

	public static String dateAdd(String type, int i) {
		SimpleDateFormat df = new SimpleDateFormat(TIME_PATTERN);
		String str = formatDateTime(TIME_PATTERN);
		Calendar c = Calendar.getInstance();
		if (type.equals("s")) {
			int s = c.get(Calendar.SECOND);
			s = s + i;
			c.set(Calendar.SECOND, s);
			str = df.format(c.getTime());
		} else if (type.equals("d")) {
			int d = c.get(Calendar.DAY_OF_MONTH);
			d = d + i;
			c.set(Calendar.DAY_OF_MONTH, d);
			str = df.format(c.getTime());
		}
		return str;
	}

	public static String getTime(int skipDay) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(new Date());

		cal.add(GregorianCalendar.DAY_OF_MONTH, skipDay);
		SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_PATTERN);

		return dateFormat.format(cal.getTime());
	}

	public static long subtraction(Date minuend, Date subtrahend) {

		long daterange = minuend.getTime() - subtrahend.getTime();
		long time = 1000 * 3600 * 24;

		return (daterange % time == 0) ? (daterange / time) : (daterange / time + 1);
	}

	public static long getM(Date date) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		return cal.get(GregorianCalendar.DAY_OF_WEEK);
	}

	public static String getLastDate(String temp) {
		if (temp == null || temp.equals("")) {
			temp = "1";
		}
		int i = Integer.parseInt(temp);
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
		Calendar grc = Calendar.getInstance();
		grc.add(GregorianCalendar.DATE, -i);
		return dateFormat.format(grc.getTime());
	}

	public static String getLastYearDate() {

		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		String year = String.valueOf(y - 1);
		return year;
	}

	public static String getLastMonthDate() {

		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH) + 1;
		String month = null;
		String year = String.valueOf(y);
		if (m > 1) {
			if (m > 10) {
				month = String.valueOf(m - 1);
			} else {
				month = "0" + String.valueOf(m - 1);
			}
		} else {
			year = String.valueOf(y - 1);
			month = String.valueOf(12);
		}

		return year + "-" + month;
	}

	public static String getLastDayDate() {
		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH) + 1;
		int d = c.get(Calendar.DAY_OF_MONTH);
		int days = 0;
		if (m > 1) {
			days = getMonthsDays(m - 1, y);
		} else {
			days = 31;
		}
		String day = null;
		String month = null;
		String year = String.valueOf(y);
		if (d > 1) {
			day = String.valueOf(d - 1);
			if (m > 9) {
				month = String.valueOf(m);
			} else {
				month = "0" + String.valueOf(m);
			}
		} else if ((d < 2) && (m < 2)) {
			day = String.valueOf(31);
			month = String.valueOf(12);
			year = String.valueOf(y - 1);
		} else if ((d < 2) && (m > 2)) {
			day = String.valueOf(days);
			if (m > 10) {
				month = String.valueOf(m - 1);
			} else {
				month = "0" + String.valueOf(m - 1);
			}
		}

		return year + "-" + month + "-" + day;
	}

	public static boolean isLeapYear(int year) {
		if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 4) == 0) && ((year % 400) == 0)) {
			return true;
		} else {
			return false;
		}
	}

	public static int getMonthsDays(int month, int year) {
		if ((isLeapYear(year) == true) && (month == 2)) {
			return 29;
		} else if ((isLeapYear(year) == false) && (month == 2)) {
			return 28;
		}

		if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10)
				|| (month == 12)) {
			return 31;
		}
		return 30;
	}

	public static String getWeekDay() {
		DateFormatSymbols symboles = new DateFormatSymbols(Locale.getDefault());
		symboles.setShortWeekdays(new String[] { "", "7", "1", "2", "3", "4", "5", "6" });
		SimpleDateFormat date = new SimpleDateFormat("E", symboles);
		return date.format(new Date());
	}

	/**
	 * String(yyyy-MM-dd) -> Date
	 *
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date parsePageDate(String strDate) {
		if (strDate == null) {
			return null;
		}
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(DATE_PATTERN_1);
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		try {
			return FORMAT_DATE_PAGE.parse(strDate);
		} catch (ParseException e) {
			throw new RuntimeException("将字符串" + strDate + "解析为" + FORMAT_DATE_DB.toPattern() + "格式的日期时发生异常:", e);
		}
	}

	/**
	 * String[yyyy-MM-dd HH:mm:ss]-> String[yyyyMMdd]
	 *
	 * @param fullTime
	 * @return
	 */
	public static String convertDbDateByFullTime(String fullTime) {
		Date fullDate = parseFullDateTime(fullTime);
		return formatDbDate(fullDate);
	}

	/**
	 * Date -> String(yyyyMMddHH)
	 *
	 * @param date
	 * @return
	 */
	public static String formatHHDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_DATE_HH = new SimpleDateFormat("yyyyMMddHH");
		return FORMAT_DATE_HH.format(date);
	}

	/**
	 * Date -> String(yyyyMMddHHmmssSSS)
	 *
	 * @param date
	 * @return
	 */
	public static String formatDbDateMilliSecond(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(FULL_TIMESTAMP);
		return FORMAT_DATE_DB.format(date);
	}

	/**
	 * Date -> String(yyyyMMdd)
	 *
	 * @param date
	 * @return
	 */
	public static String formatDbDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(DATE_PATTERN_1);
		return FORMAT_DATE_DB.format(date);
	}

	/**
	 * 取得当前年字符串; 时间格式:yyyy
	 *
	 * @return
	 */
	public static String getCurrentYear() {
		SimpleDateFormat FORMAT_DATE_YEAR = new SimpleDateFormat("yyyy");
		return FORMAT_DATE_YEAR.format(new Date());
	}

	public static String getCurrentMonth() {
		SimpleDateFormat FORMAT_DATE_YEAR = new SimpleDateFormat("MM");
		return FORMAT_DATE_YEAR.format(new Date());
	}

	/**
	 * 验证日期字符串是否为[yyyyMM]格式
	 *
	 * @param dateStr
	 * @return
	 */
	public static boolean isDBDateYM(String dateStr) {
		SimpleDateFormat FORMAT_DATE_YM = new SimpleDateFormat(DATE_PATTERN_3);
		try {
			FORMAT_DATE_YM.parse(dateStr);
		} catch (ParseException e) {
			return false;
		}
		return true;
	}

	/**
	 * 验证日期字符串是否为[yyyyMMdd]格式
	 *
	 * @param dateStr
	 * @return
	 */
	public static boolean isDBDateStr(String dateStr) {
		SimpleDateFormat FORMAT_DATE_DB = new SimpleDateFormat(DATE_PATTERN_1);
		try {
			FORMAT_DATE_DB.parse(dateStr);
		} catch (ParseException e) {
			return false;
		}
		return true;
	}

	/**
	 * 对齐日期对象到指定精度
	 *
	 * @param date  日期对象
	 * @param field 要对齐的时间域，参考Calendar中field的定义
	 * @return 对齐后的日期
	 */
	public static Date roundDate(Date date, int field) {
		if (date == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		roundDate(cal, field);

		return cal.getTime();
	}

	public static void roundDate(Calendar cal, int field) {
		boolean clearFlag = false;
		for (int i = 0; i < TIME_FIELD_LEVELS.length; i++) {
			if (clearFlag) {
				cal.set(TIME_FIELD_LEVELS[i], cal.getMinimum(TIME_FIELD_LEVELS[i]));
			} else if (TIME_FIELD_LEVELS[i] == field) {
				clearFlag = true;
			}
		}
	}

	/**
	 * 调整日期对象
	 *
	 * @param date   日期对象
	 * @param field  时间域，参考Calendar中field的定义
	 * @param amount 调整数量
	 * @return 调整后的日期对象
	 */
	public static Date rollDate(Date date, int field, int amount) {
		if (date == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.add(field, amount);
		return cal.getTime();
	}

	/**
	 * 调整SQL日期对象
	 *
	 * @param date   日期对象
	 * @param field  时间域，参考Calendar中field的定义
	 * @param amount 调整数量
	 * @return 调整后的日期对象
	 */
	public static java.sql.Date rollDate(java.sql.Date date, int field, int amount) {
		if (date == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.add(field, amount);
		return new java.sql.Date(cal.getTimeInMillis());
	}

	/**
	 * 获得日期对象的时间域值
	 *
	 * @param field 时间域，参考Calendar中field的定义
	 * @return 对应时间域的值
	 */
	public static int getDateField(Date date, int field) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(field);
	}

	/**
	 * 获得修改时间域值后的日期对象
	 *
	 * @param date  日期对象
	 * @param field 时间域，参考Calendar中field的定义
	 * @param value 时间域的值
	 * @return 修改后的日期对象
	 */
	public static Date setDateField(Date date, int field, int value) {
		if (date == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.set(field, value);
		return cal.getTime();
	}

	/**
	 * 获得修改时间域值后的日期对象
	 *
	 * @param date  日期对象
	 * @param field 时间域，参考Calendar中field的定义
	 * @param value 时间域的值
	 * @return 修改后的日期对象
	 */
	public static java.sql.Date setDateField(java.sql.Date date, int field, int value) {
		if (date == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.set(field, value);
		return new java.sql.Date(cal.getTimeInMillis());
	}

	/**
	 * 根据给定参数打造时间对象, 毫秒数为零.
	 *
	 * @param year  年. 例 2009
	 * @param month 月. 从1开始, 例 1
	 * @param date  日. 从1开始, 例 10
	 * @return 日期对象. 如果参数错误, 则返回null, 例 year=0; month=2, date=30
	 */
	public static Date getDate(int year, int month, int date) {
		return getDate(year, month, date, 0, 0, 0);
	}

	/**
	 * 根据给定参数打造时间对象, 毫秒数为零.
	 *
	 * @param year      年. 例 2009
	 * @param month     月. 从1开始, 例 1
	 * @param date      日. 从1开始, 例 10
	 * @param hourOfDay 小时. 24小时制, 从0开始, 例 23
	 * @param minute    分. 从0开始, 例 59
	 * @param second    秒. 从0开始, 例 59
	 * @return 日期对象. 如果参数错误, 则返回null, 例 year=0; month=2, date=30
	 */
	public static Date getDate(int year, int month, int date, int hourOfDay, int minute, int second) {
		try {
			Calendar c = Calendar.getInstance();
			c.setLenient(false);
			c.clear();
			c.set(year, month - 1, date, hourOfDay, minute, second);

			return c.getTime();
		} catch (Exception ex) {
			return null;
		}
	}

	// 获取当前年
	public static String getYear() {
		Calendar c = Calendar.getInstance();
		return String.valueOf(c.get(Calendar.YEAR));
	}

	// 获取当前月
	public static String getMonth() {
		Calendar c = Calendar.getInstance();
		return String.valueOf(c.get(Calendar.MONTH));
	}

	// 获取当前日
	public static String getDay() {
		Calendar c = Calendar.getInstance();
		return String.valueOf(c.get(Calendar.DAY_OF_MONTH));
	}

	public static Date parse(String pattern, Locale locale, String str) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
		return sdf.parse(str);
	}

	public static Date parse(String pattern, String str) throws ParseException {
		return parse(pattern, Locale.getDefault(), str);
	}

	public static String format(String pattern, Locale locale, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
		return sdf.format(date);
	}

	public static String format(String pattern, Date date) {
		return format(pattern, Locale.getDefault(), date);
	}

	/**
	 * 取得当前日期时间，省略毫秒数。 yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static Date getCurrentDateTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取当前时间(带毫秒数)
	 *
	 * @return
	 */
	public static Date getCurrentDateFullTime() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}

	/**
	 * 获取java.util.Date副本
	 *
	 * @param date 日期对象
	 * @return 日期对象
	 */
	public static Date getDate(Date date) {
		return (date != null ? new Date(date.getTime()) : null);
	}

	/**
	 * 根据给定参数打造java.sql.Date对象
	 *
	 * @param year
	 * @param month
	 * @param date
	 * @return 日期对象. 如果参数错误, 则返回null
	 */
	public static java.sql.Date getSQLDate(int year, int month, int date) {
		try {
			Calendar c = Calendar.getInstance();
			c.setLenient(false);
			c.clear();
			c.set(year, month - 1, date);

			return new java.sql.Date(c.getTimeInMillis());
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 在指定日期上增加指定时间(单位: 毫秒)
	 *
	 * @param date
	 * @param millis
	 * @return
	 */
	public static Date addMillis(Date date, long millis) {
		return new Date(date.getTime() + millis);
	}

	/**
	 * 获取两个日期间隔的天数, 非正数天向上取整, 为正代表date1大于date2
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int daysBetween(Date date1, Date date2) {
		return (int) ((date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000));
	}

	/**
	 * 获取两个日期间隔的月份数, 为正代表date1大于date2
	 *
	 * @param date1
	 * @param date2
	 */
	public static int monthsBetween(Date date1, Date date2) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date1);

		Calendar c2 = Calendar.getInstance();
		c2.setTime(date2);

		return ((c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR)) * 12
				+ (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH)));
	}

	/**
	 * 获取两个日期间隔的年数，为正代表date1大于date2
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int yearsBetween(Date date1, Date date2) {
		return (getDateField(date1, Calendar.YEAR) - getDateField(date2, Calendar.YEAR));
	}

	public static int getCurrentQuarter(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month = cal.get(Calendar.MONTH);
		return month / 3;
	}

	public static Date nextQuarterFirstMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month = cal.get(Calendar.MONTH);
		month = month % 3;
		cal.add(Calendar.MONTH, 3 - month);
		return cal.getTime();
	}

	public static boolean inTerm(String term, Date startTime, Date nowTime) {
		Matcher matcher = TERM_PATTERN.matcher(term);
		boolean matchFound = matcher.find();
		if (matchFound == false) {
			throw new IllegalArgumentException("Illegal term=[" + term + "].");
		}

		int num = Integer.valueOf(matcher.group(1), 10);
		String unit = matcher.group(2);

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(startTime.getTime());
		cal.add(PERIOD_UNITS.get(unit), num);

		return cal.getTime().compareTo(nowTime) >= 0;
	}

	/**
	 * 得到指定时间的00:00:00
	 *
	 * @param date
	 * @return
	 */
	public static Date getStartDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	/**
	 * 得到指定时间的23:59:59
	 *
	 * @param date
	 * @return
	 */
	public static Date getEndDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTime();
	}

	/**
	 * 得到指定时间的23:30:00
	 *
	 * @param date
	 * @return
	 */
	public static Date getEndDateHalf(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 30);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	/**
	 * 返回当前日期加addDay天
	 *
	 * @return
	 */
	public static Date getDayAddDate(int addDay) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, addDay);
		return c.getTime();
	}

	/**
	 * 获取当前时间+hour小时的时间
	 *
	 * @param hour
	 * @return
	 */
	public static Date getHourAdd(int hour) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, hour);
		return c.getTime();
	}

	public static Date getMinuteAdd(int minute) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MINUTE, minute);
		return c.getTime();
	}

	/**
	 * 指定时间加i天
	 * @param today 指定的时间
	 * @param i  加的天数
	 * @return
	 */
	public static Date stipulateDateAddDay(Date today,int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(today);
		c.add(Calendar.DATE,i);
		return c.getTime();
	}

	public static String getWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(week_index<0){
				week_index = 0;
		}
		return weeks[week_index];
	}

	/**
	 * 当周的第一天
	 * @param date
	 * @return
	 */
	public static String weekStart(Date date) {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date);
		curCal.set(Calendar.DAY_OF_WEEK, 1);
		Date beginTime = curCal.getTime();
		String sTime = FORMAT_DATE_PAGE.format(beginTime) + " 00:00:00";
		return sTime;
	}

	/**
	 * 当月第一天
	 *
	 * @return
	 */
	public static String monthStart(Date date) {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date);
		curCal.set(Calendar.DAY_OF_MONTH, 1);
		Date beginTime = curCal.getTime();
		String sTime = FORMAT_DATE_PAGE.format(beginTime) + " 00:00:00";
		return sTime;
	}

	/**
	 * 当月最后一天
	 *
	 * @return
	 */
	public static String monthEnd(Date date) {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date);
		curCal.set(Calendar.DATE, 1);
		curCal.roll(Calendar.DATE, -1);
		Date endTime = curCal.getTime();
		String eTime = FORMAT_DATE_PAGE.format(endTime) + " 23:59:59";
		return eTime;
	}

	/**
	 * 当年第一天
	 * @param date
	 * @return
	 */
	public static String yearStart(Date date) {
		SimpleDateFormat FORMAT_DATE_PAGE = new SimpleDateFormat(DATE_PATTERN);
		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date);
		curCal.set(Calendar.DAY_OF_YEAR, 1);
		Date beginTime = curCal.getTime();
		String sTime = FORMAT_DATE_PAGE.format(beginTime) + " 00:00:00";
		return sTime;
	}

	public static void main(String[] args) {
		System.out.println(yearStart(new Date()));
		Date startDate = DateUtil.parseFullDateTime(DateUtil.monthStart(getCurrentDateTime()));
		Date endDate = DateUtil.parseFullDateTime(DateUtil.monthEnd(getCurrentDateTime()));
		int daysBetween = DateUtil.daysBetween(endDate, startDate);
		System.out.println(startDate);
		System.out.println(endDate);
		System.out.println(daysBetween);
		
		for (int i=0;i<=daysBetween;i++) {
			Date addDay = DateUtil.stipulateDateAddDay(startDate,i);
			Date addDayEndDate = DateUtil.getEndDate(addDay);
			System.out.println(addDay);
			System.out.println(addDayEndDate);
		}
		
//		System.out.println(DateUtil.getStartDateStr(new Date()));
//		System.out.println(DateUtil.getEndDateStr(new Date()));
//		System.out.println(DateUtil.getStartDate(DateUtil.getDayAddDate(1)));
//		System.out.println(DateUtil.getStartDateStr(DateUtil.getDayAddDate(1)));
//
//		Date sDate = DateUtil.getDayAddDate(1);
//		System.out.println(sDate);
//		Date minDate = DateUtil.getStartDate(sDate);
//		Date maxDate = DateUtil.getEndDate(sDate);
//		System.out.println("minDate:"+minDate);
//		System.out.println("maxDate:"+maxDate);
//		if (sDate.after(minDate) && sDate.before(maxDate)) {
//			System.out.println("11111111111");
//		}
//		if ((sDate.getTime() > minDate.getTime()) && (sDate.getTime() < maxDate.getTime())) {
//			System.out.println("11111111111");
//		}
//		if (new Date().getTime() >= new Date().getTime()) {
//			System.out.println("11111111111");
//		}
//
		System.out.println(DateUtil.formatDbDateMilliSecond(DateUtil.stipulateDateAddDay(new Date(),10)));
//		if ("20201220".equals("20201220")) {
//			System.out.println("2221111");
//		}

		System.out.println(DateUtil.daysBetween(new Date(),DateUtil.parseFullDateTime("2021-09-16 10:00:00")));

		Date beginTime = new Date();
		Date endTime = new Date();
		Integer totalPeriodNum = 10;
		Integer lockCycle = 0;
		if (lockCycle > 0) {
			beginTime = DateUtil.getStartDate(DateUtil.getDayAddDate(lockCycle));
			endTime = DateUtil.getStartDate(DateUtil.getDayAddDate(lockCycle + totalPeriodNum));
		} else {
			beginTime = DateUtil.getStartDate(DateUtil.getDayAddDate(1));
			endTime = DateUtil.getStartDate(DateUtil.getDayAddDate(totalPeriodNum));
		}

		System.out.println(DateUtil.formatFullDate(beginTime));
		System.out.println(DateUtil.formatFullDate(endTime));

		//下次挖矿时间
//		System.out.println(DateUtil.getStartDate(DateUtil.getDayAddDate(1)));
		//合约到期时间
//		System.out.println(DateUtil.getDayAddDate(365));

//		System.out.println(DateUtil.formatFullDate(DateUtil.getEndDateHalf(new Date())));

//		System.out.println(DateUtil.formatFullDate(DateUtil.stipulateDateAddDay(DateUtil.parseFullDateTime("2021-05-25 12:12:12"), 7)));
	}

}
